<?php
namespace App\Controller;

use Hyperf\HttpServer\Annotation\AutoController;
/**
 * @AutoController
 */
class LogController extends AbstractController
{
    public function show()
    {
        $amount = $this->request->input('amount',50);
        $date = date('Y-m-d');
        $begin_time = $this->request->input('begin_time','');
        $end_time = $this->request->input('end_time','');
        if($this->request->input('date')) {
            $date = $this->request->input('date');
        }
        $level = strtoupper($this->request->input('level','all'));//info error debug warning
        $name = strtolower($this->request->input('name','all'));//app queue
        $keyword = $this->request->input('keyword');
        $path = $this->request->input('path', '');
        if(!empty($path))
        {
            $content = file_get_contents(BASE_PATH.'/runtime/logs/'.$path.'/hyperf-'.$date.'.log');
        }
        $arr = explode("\n",$content);
        $total = count($arr);
        $html = '';

        if($keyword) {
            //有关键字
            foreach($arr as $k=>$v) {
                if(preg_match('/'.$keyword.'/',$v)){
                    $i = $k;
                    $j = 0;
                    while($i < $total && $j < $amount) {
                        if($arr[$i]) {
                            $satisfied = true;
                            if($name != 'all' && preg_match('/\d\]\s(.*?)\..*/',$arr[$i],$matches)) {
                                if($matches[1] != $name){
                                    $satisfied = false;
                                }
                            }
                            if($level != 'ALL' && preg_match('/\d\]\s.*\.(.*?):.*/',$arr[$i],$matche)){
                                //.*? 表示匹配不贪婪
                                if($matche[1] != $level) {
                                    $satisfied = false;
                                }
                            }
                            // time select
                            if($begin_time) {
                                $log_time = substr($arr[$i],12,8);
                                if($log_time < $begin_time){
                                    $satisfied = false;
                                }
                            }
                            if($end_time) {
                                $log_time = substr($arr[$i],12,8);
                                if($log_time > $end_time){
                                    $satisfied = false;
                                }
                            }
                            if($satisfied) {
                                //顺序
                                $html .= $arr[$i]."\r\n";
                                $j++;
                            }
                        }
                        $i++;
                    }
                    break;
                }
            }
        }else{
            //no keyword
            $i = $total - 1;
            $j = 0;
            while($i >= 0 && $j < $amount) {
                if($arr[$i]) {
                    $satisfied = true;
                    if($name != 'all' && preg_match('/\d\]\s(.*?)\..*/',$arr[$i],$matches)) {
                        if($matches[1] != $name){
                            $satisfied = false;
                        }
                    }
                    if($level != 'ALL' && preg_match('/\d\]\s.*\.(.*?):.*/',$arr[$i],$matche)){
                        //.*? 表示匹配不贪婪
                        if($matche[1] != $level) {
                            $satisfied = false;
                        }
                    }
                    // time select
                    if($begin_time) {
                        $log_time = substr($arr[$i],12,8);
                        if($log_time < $begin_time){
                            $satisfied = false;
                        }
                    }
                    if($end_time) {
                        $log_time = substr($arr[$i],12,8);
                        if($log_time > $end_time){
                            $satisfied = false;
                        }
                    }
                    if($satisfied) {
                        //倒叙
                        $html = $arr[$i]."\r\n".$html;
                        $j++;
                    }
                }
                $i--;
            }
        }


        return $this->response->raw($html);
    }
}